//
//  UIViewController+NSUIViewController.m
//  XinDong
//
//  Created by tt on 12-11-30.
//  Copyright (c) 2012年 HengYongKeJi. All rights reserved.
//

#import "NSUIViewController.h"

@implementation UIViewController (NSUIViewController)
static UIView *activityIndicatorView = nil;
static NSUInteger activityIndicatorViewCount = 0;

- (void)addUIActivityIndicatorView {
	if (activityIndicatorViewCount == 0) {     //表示还没有添加IndicatorView
		
		activityIndicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320.f, 367.f)];		//表示挡住当前界面的全部显示，不包括NavigationBar, TabBar.
		
		UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 60)];
		shadowView.center = activityIndicatorView.center;
		shadowView.backgroundColor = [UIColor blackColor];
		shadowView.layer.cornerRadius = 10.f;
		shadowView.alpha = 0.8f;
		[activityIndicatorView addSubview:shadowView];
		
		UIActivityIndicatorView *activity =  [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
		activity.center = activityIndicatorView.center;
		[activityIndicatorView addSubview:activity];
		[activity startAnimating];
		
		activityIndicatorView.backgroundColor = [UIColor clearColor];
		[self.view addSubview:activityIndicatorView];
	}
	
	activityIndicatorViewCount++;
}

//移除UIActivityIndicatorView
- (void)removeUIActivityIndicatorView {
	if (activityIndicatorViewCount == 0) {		//这里做容错处理，防止在已经没有activityIndicatorView的情况下作移除操作
		return;
	}
	else if (activityIndicatorViewCount == 1) {   //表示有一个IndicatorView，需要移除
		activityIndicatorView.alpha = 1.0f;
		[UIView beginAnimations:nil context:nil];
		[activityIndicatorView removeFromSuperview];
		[UIView commitAnimations];
        //		self.view.userInteractionEnabled = YES;
		activityIndicatorViewCount = 0;
	}
	else {											//表示还有多个没被移除的IndicatorView，使计数减一即可
		activityIndicatorViewCount--;
	}
}
@end
